package com.Liux.blog.service.impl;

import com.Liux.blog.dao.UserMapper;
import com.Liux.blog.entity.User;
import com.Liux.blog.service.UserService;
import com.Liux.blog.util.MD5Utils;
import com.Liux.blog.vo.ResultStatus;
import com.Liux.blog.vo.ResultVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.util.Date;
import java.util.List;
import java.util.UUID;

@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private UserMapper userMapper;


    @Override
    public List<User> loginPwd(String name, String pwd) {
        Example example = new Example(User.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("loginName",name);
        criteria.andEqualTo("password",pwd);
        List<User> users = userMapper.selectByExample(example);
        return users;
    }

    @Override
    public User getUserById(String userId) {
        Example example = new Example(User.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("id",userId);
        List<User> users = userMapper.selectByExample(example);
        return users.get(0);
    }

    @Override
    public ResultVo addUser(User user) {
        user.setId(UUID.randomUUID().toString());
        user.setCreateTime(new Date());
        user.setPassword(MD5Utils.md5(user.getPassword()));
        int i = userMapper.insert(user);
        if(i>0){
            return new ResultVo(ResultStatus.OK,"success",user);
        }else{
            return new ResultVo(ResultStatus.NO,"fail",null);
        }
    }
}
